package com.example.enterprisemanager;

import android.app.Application;
import android.support.annotation.Nullable;

import com.example.enterprisemanager.base.LogCatStrategy;
import com.example.enterprisemanager.util.SharedPreferencesUtil;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobConfig;
import me.yokeyword.fragmentation.Fragmentation;

public class APP extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initLogger();

        Fragmentation.builder()
                // show stack view. Mode: BUBBLE, SHAKE, NONE
                .stackViewMode(Fragmentation.BUBBLE)
                .debug(BuildConfig.DEBUG)
                .handleException(e -> Logger.d("Fragmentation message " + e.getMessage()))
                .install();

        BmobConfig config = new BmobConfig.Builder(this)
                .setApplicationId("a1c8760bb738f722910ff06ed501faaf")
                .setConnectTimeout(15)//设置超时时间 （秒）
                .setUploadBlockSize(1024 * 1024)//文件分片上传时每片的大小（单位字节）
                .setFileExpiration(1800)//文件的过期时间(单位为秒)
                .build();
        Bmob.initialize(config);

        SharedPreferencesUtil.getInstance().init(this);

        initLeakCanary();
    }

    private RefWatcher mRefWatcher;

    /**
     * 初始化LeakCanary
     */
    private void initLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        mRefWatcher = LeakCanary.install(this);
    }

    public RefWatcher getRefWatcher() {
        return mRefWatcher;
    }


    /**
     * 初始化Logger
     */
    private void initLogger() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)
                .methodCount(2)
                .logStrategy(new LogCatStrategy())
                .tag("EnterpriseManager")
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, @Nullable String tag) {
                return BuildConfig.DEBUG;
            }
        });
    }
}
